enyo.kind({
  name: "PsnTrophies.Trophies",
  kind: enyo.VFlexBox,
  classes: "onyx",
  events: { 
      onSelect: ""
  },
  components: [
	  {name: "getTrophies", kind: "WebService",
	      onSuccess: "gotTrophies",
	      onFailure: "gotTrophiesFailure"},
	  {name: "trophyScroller", kind: "Scroller", flex: 1, autoHorizontal: false, horizontal: false, className: "enyo-bg", components: [
		  {caption: "All Trophies", kind: "RowGroup", components: [
	  	  {name: "trophyList", kind: "VirtualRepeater", onSetupRow: "getTrophyListItem", components: [
	  	    {kind: "Item", layoutKind: "VFlexLayout", components: [
	  			  {kind: "Control", layoutKind: "HFlexLayout", components: [
	  					{name: "trophyImage", kind: "Image", height: "50px", width: "50px"},
	  					{name: "trophyTypeImage", kind: "Image"},
	  				  {kind: "Control", layoutKind: "VFlexLayout", components: [
	  					  {name: "title", style: "font-size: 200%; font-weight: bold"},
	  					  {name: "description"}
	  			    ]}
	  			  ]}
	  		  ]}
	  		]}
	    ]}
	  ]}
  ],
	create: function() {
	  this.inherited(arguments);
	  this.curTrophies = [];
	  this.psnId = "";
	},
	getTrophiesService: function(psnId, selectedGame) {
	  enyo.log("Requesting trophies for " + psnId + ", for game: " + selectedGame.Title);  
	  var getTrophiesUrl = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%20%3D%20%22http%3A%2F%2Fwww.psnapi.com.ar%2Fps3%2Fapi%2Fpsn.asmx%2FgetTrophies%3FsPSNID%3D"	
	  						+ psnId + "%26sGameId%3D" + selectedGame.Id + "%22&format=json&callback=";
	  this.$.getTrophies.setUrl(getTrophiesUrl);
	  
	  enyo.log("Getting trophies with url: " + getTrophiesUrl);
	  this.$.getTrophies.call();
	},
	gotTrophies: function(inSender, inResponse) {
	  enyo.log("gotTrophies(): at start");
	  this.curTrophies = inResponse.query.results.ArrayOfTrophy.Trophy;
	  enyo.log("gotTrophies(): Got " + this.curTrophies.length + " trophies for the game");
	  
	  this.$.trophyList.render();
	},
	gotTrophiesFailure: function(inSender, inResponse) {
	  enyo.log("Failure in getTrophies: " + inResponse.error.description);
	},
	getTrophyListItem: function(inSender, inIndex) {
		enyo.log("getTrophyListItem(): inIndex is " + inIndex);
	  var r = this.curTrophies[inIndex];
	  enyo.log(r);
	  if (r) {
	      
	      if (r.DateEarned != null) {
	      	this.$.trophyImage.setSrc(r.Image);
	      }	else { 
	      	this.$.trophyImage.setSrc("trophy_locksmall.png");
	      }
	      
	      if (r.TrophyType == "PLATINUM")
	        this.$.trophyTypeImage.setSrc("game-plat.png");
	      else if (r.TrophyType == "GOLD")
	        this.$.trophyTypeImage.setSrc("game-gold.png");
	      else if (r.TrophyType == "SILVER")
	        this.$.trophyTypeImage.setSrc("game-silver.png");
	      else if (r.TrophyType == "BRONZE")
	        this.$.trophyTypeImage.setSrc("game-bronze.png");
	      
	      this.$.title.setContent(r.Title);
	      this.$.description.setContent(r.Description);
	      
	      return true;
	  }
	},
});
	